//通用方法
export const showToast = (title, duration = 2000, icon = 'none') => {
	uni.showToast({
		title: title,
		duration: duration,
		icon: icon,
		mask: true
	});
}
export const showLoading = (title) => {
	uni.showLoading({
	    title: title,
		mask: true
	});
}

export const hideLoading = () => {
	uni.hideLoading()
}

/**
 * 判断是否是数字
 *
 * @param {[]} value 数据
 */
export const isNumber = (value) => {
  return !isNaN(parseFloat(value)) && !isNaN(Number(value));
}

/**
 * 处理自定义表单中数据
 *
 * @param formData 表单原始数据
 * @param widgetList 表单组件元素列表
 * @param arr 表单组件数组，不包含grid、table、tab、card等组件
 */
export const transferFormData = (formData, widgetList, arr) => {
  transferWidget(widgetList, arr);

  arr.forEach((widget) => {
	let key = widget.options.name;
	let type = widget.type;
	switch (type)
	{
	  case "file-upload":
	  if (formData[key]){
		let json = JSON.parse(formData[key]);
		for (let i = 0; i < json.length; i++){
		  json[i].url = json[i].response.data[0].url;
		}
		formData[key] = json;
	  }
	  widget.options.defaultValue = formData[key];
	  break;
	  case "number":
	  case "rate":
	  case "slider":
	    if (formData[key] && isNumber(formData[key])){
		  formData[key] = Number(formData[key])
	    }
		widget.options.defaultValue = formData[key];
	  break;
	  case "radio":
	    if (formData[key] && isNumber(formData[key])){
		  formData[key] = Number(formData[key])
	    }
		widget.options.defaultValue = formData[key];
	    widget.options.optionItems.forEach((s) => { s['text'] = s['label']});
	  break;
	  case "switch":
	    if (formData[key] === "true"){
		  formData[key] = true;
	    }
	    else if (formData[key] === "false"){
		  formData[key] = false;
	    }
		widget.options.defaultValue = formData[key];
	  break;
	  case "time-range":
	  case "date-range":
	  case "cascader":
	    if (formData[key] && formData[key].includes('[') && formData[key].includes(']')){
		  let str = formData[key].substring(1, formData[key].length - 1);
		  formData[key] = str.split(',');
		  for(let i = 0; i < formData[key].length; i++){
		    formData[key][i] = formData[key][i].trim();
		    if (isNumber(formData[key][i])){
			  formData[key][i] = Number(formData[key][i]);
		    }
		  }
	    }
	    else if (formData[key] && isNumber(formData[key])){
		  formData[key] = Number(formData[key])
	    }
		widget.options.defaultValue = formData[key];
	  break;
	  case "checkbox":
	  case "select":
	    if (formData[key] && formData[key].includes('[') && formData[key].includes(']')){
		  let str = formData[key].substring(1, formData[key].length - 1);
		  formData[key] = str.split(',');
		  for(let i = 0; i < formData[key].length; i++){
		    formData[key][i] = formData[key][i].trim();
		    if (isNumber(formData[key][i])){
			  formData[key][i] = Number(formData[key][i]);
		    }
		  }
	    }
	    else if (formData[key] && isNumber(formData[key])){
		  formData[key] = Number(formData[key])
	    }
		widget.options.defaultValue = formData[key];
	    widget.options.optionItems.forEach((s) => { s['text'] = s['label']});
	  break;
	  default:
	    widget.options.defaultValue = formData[key];
	  break;
	}
  })
}

/**
 * 转换组件清单为键值对和数组
 *
 * @param widgetList 表单组件元素列表
 * @param arr 表单组件数组，不包含grid、table、tab、card等组件
 */
export const transferWidget = (widgetList, arr) => {
  for(let i = 0; i < widgetList.length; i++){
    let widget = widgetList[i];
    let type = widget.type;
    switch (type)
    {
      case "grid":
        let cols = widget.cols;
        for (let j = 0 ; j < cols.length; j++){
          let col = cols[j];
          transferWidget(col.widgetList, arr);
        }
        break;
      case "table":
        let rows = widget.rows;
        for (let j = 0 ; j < rows.length; j++){
          let row = rows[j];
          let rowCols = row.cols;
          for (let k = 0; k < rowCols.length; k++){
            let rowCol = rowCols[k];
            transferWidget(rowCol.widgetList, arr);
          }
        }
        break;
      case "tab":
        let tabs = widget.tabs;
        for (let j = 0 ; j < tabs.length; j++){
          let tab = tabs[j];
          transferWidget(tab.widgetList, arr);
        }
        break;
      case "card":
        transferWidget(widget.widgetList, arr);
        break;
      default:
        arr.push(widget);
        break;
    }
  }
}
